package view;

import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.StageStyle;

import java.io.IOException;

public class ChatListItemView extends  Window {

    //810
    //660

    public static final int DIRECTION_LEFT = 1;
    public static final int DIRECTION_RIGHT = 2;

    private AnchorPane pane;

    public ChatListItemView(double width, double height) throws IOException {
        root = FXMLLoader.load(getClass().getClassLoader().getResource("chatListItem.fxml"));
        Scene scene = new Scene(root, width, height);
        scene.setFill(Color.TRANSPARENT);
        setScene(scene);
        initStyle(StageStyle.TRANSPARENT);
        setResizable(false);
        setTitle("ChatItem");

        init();

        pane.setMaxSize(width,height);

    }

    private void init() {
        pane = (AnchorPane)findViewById("rootPane");
    }

    public AnchorPane getPane() {
        return pane;
    }

    public void setName(String name, int direction) {
        if (direction == DIRECTION_LEFT) {
            ((Pane)findViewById("leftPane")).setVisible(true);
            ((Pane)findViewById("rightPane")).setVisible(false);
            ((Label)findViewById("lname")).setText(name);
        }
        if (direction == DIRECTION_RIGHT) {
            ((Pane)findViewById("rightPane")).setVisible(true);
            ((Pane)findViewById("leftPane")).setVisible(false);
            ((Label)findViewById("rname")).setText(name);
        }
    }

    public void setPortrait(String url, int direction) {
        if (direction == DIRECTION_LEFT) {
            ((ImageView)findViewById("leftImg")).setStyle("-fx-image: url(" + url + ");");
        }
        if (direction == DIRECTION_RIGHT) {
            ((ImageView)findViewById("rightImg")).setStyle("-fx-image: url(" + url + ");");
        }
    }

    public void setMsg(String msg, int direction) {
        if (direction == DIRECTION_LEFT) {
            ((Label)findViewById("lMsg")).setText(msg);
        }
        if (direction == DIRECTION_RIGHT) {
            ((Label)findViewById("rMsg")).setText(msg);
        }
    }

    public void setMsgLabelSize(double width, double height, int direction) {
        if (direction == DIRECTION_LEFT) {
            ((AnchorPane)findViewById("rootPane")).setPrefHeight(30 + height);
            ((Label)findViewById("lMsg")).setPrefSize(width, height);
        }
        if (direction == DIRECTION_RIGHT) {
            ((AnchorPane)findViewById("rootPane")).setPrefHeight(30 + height);
            ((Label)findViewById("rMsg")).setPrefSize(width, height);
        }
    }

    @Override
    public void quit() {

    }

    @Override
    public void minimize() {

    }
}
